package com.ruoyi.ai.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ai.domain.vo.WxDialogueVo;
import com.ruoyi.ai.domain.bo.WxDialogueBo;
import com.ruoyi.ai.service.IWxDialogueService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 对话管理
 *
 * @author ruoyi
 * @date 2024-04-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/dialogue")
public class WxDialogueController extends BaseController {

    private final IWxDialogueService iWxDialogueService;

    /**
     * 查询对话管理列表
     */
    @SaCheckPermission("ai:dialogue:list")
    @GetMapping("/list")
    public TableDataInfo<WxDialogueVo> list(WxDialogueBo bo, PageQuery pageQuery) {
        return iWxDialogueService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出对话管理列表
     */
    @SaCheckPermission("ai:dialogue:export")
    @Log(title = "对话管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxDialogueBo bo, HttpServletResponse response) {
        List<WxDialogueVo> list = iWxDialogueService.queryList(bo);
        ExcelUtil.exportExcel(list, "对话管理", WxDialogueVo.class, response);
    }

    /**
     * 获取对话管理详细信息
     *
     * @param dialogueId 主键
     */
    @SaCheckPermission("ai:dialogue:query")
    @GetMapping("/{dialogueId}")
    public R<WxDialogueVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long dialogueId) {
        return R.ok(iWxDialogueService.queryById(dialogueId));
    }

    /**
     * 新增对话管理
     */
    @SaCheckPermission("ai:dialogue:add")
    @Log(title = "对话管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxDialogueBo bo) {
        return toAjax(iWxDialogueService.insertByBo(bo));
    }

    /**
     * 修改对话管理
     */
    @SaCheckPermission("ai:dialogue:edit")
    @Log(title = "对话管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxDialogueBo bo) {
        return toAjax(iWxDialogueService.updateByBo(bo));
    }

    /**
     * 删除对话管理
     *
     * @param dialogueIds 主键串
     */
    @SaCheckPermission("ai:dialogue:remove")
    @Log(title = "对话管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dialogueIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] dialogueIds) {
        return toAjax(iWxDialogueService.deleteWithValidByIds(Arrays.asList(dialogueIds), true));
    }
}
